(function($) {
	$(function() {
				$("[hte-include]").each(function() {
							var el = $(this);
							var src = el.attr("hte-include");
							$.get(src, function(html) {
										// $(html).insertAfter(el);
										el.html(html);
									}, 'html');
						});
			});
	$(function() {
				$(document).on("click.hte-app-nav-api", "[hte-nav]",
						function(event) {
							var nav = $(event.target);
							if (!nav.data("hte-nav")) {
								nav = nav.parent("[hte-nav]");
							}
							var container = nav.parents("[hte-app]");
							var app = window[container.attr("hte-app")];
							if (app) {
								app.load(nav.attr("hte-nav"));
							}
							nav.parents(".hte-navbar").find(".active")
									.removeClass("active");
							nav.addClass("active");
						});
			});
})(jQuery);

(function($) {

	/***************************************************************************
	 * 创建APP
	 **************************************************************************/
	var app = Hte.App = function(el) {
		this.id = el.attr("hte-app");
		this.el = el;
		el.data("app", this);
		window[this.id] = this;
		this.el.find("[hte-view]").height(this.el.height());
		if (el.attr("hte-index")) {
			this.load(el.attr("hte-index"));
		}
		var app = this;
		$(window).resize(function() {
				app.el.find("[hte-view]").height(app.el.height());
				window.setTimeout(function() {
							app.layout();
						}, 100);
				});

		var initFn = el.attr("hte-app-init"), fn = window[initFn];
		fn && fn();

		// 触发一个初始化事件
		this.trigger("init");
		this.layout();
	};

	app.prototype.events = {};
	app.prototype.handlers = {};

	app.prototype.load = function(url) {
		if (!url)
			return;
		var app = this;
		var view = app.el.find("[hte-view]");
		NProgress.start();
		$.get(url, function(html) {
					app.trigger("unload");
					view.html(html);
					app.trigger("load");
					app.layout();
				}, "html").complete(function() {
					NProgress.done();
				}).error(function(xhr, message, status, statusCode) {
			/*
			 * if(parent.HteOS){ parent.HteOS.Messager.alert("提示","加载失败");
			 * }else{ alert("加载失败"); }
			 */
			view
					.html("<div class='hte-error'><span class='glyphicon glyphicon-remove-sign'></span>无法加载页面:"
							+ xhr.status + "</div>");
		});
		/*
		 * view.find("[hte-layout]").each(function(index,container){ new
		 * Hte.Layout($(container)); });
		 */
	}
	// 自动布局
	app.prototype.layout = function() {

		var app = this, layout = app.el.find(".hte-layout");
		if (layout.length < 0) {
			return false;
		}
		var subs = layout.children("div.hte-panel"), remain = layout.height();
		//console.log("view height : " + remain);
		var fixedItems = [];
		subs.each(function(index, el) {
					var sub = $(el);
					// if(sub.hasAttribute("hte-layout-fixed") ||
					// sub.hasClass("bootstrap-table")){
					if (sub.hasClass("hte-fixed")) {
						fixedItems.push(sub);
						return;
					}
					var height = sub.outerHeight();
					//console.log("panel height:" + height);
					remain = remain - height;
				});
		fixedItems.forEach(function(item) {
					//var table = item.find("[data-toggle='table']");
					//table.options.height = remain;
					if(item.hasClass("hte-table")){
						item.find(".hte-data-table").bootstrapTable(
							'resetView', {
								height : remain
							});
					}else{
						item.css("height",remain);
					}
				});
	}

	app.prototype.on = function(event, fn, scope, once) {
		var events = event;
		if (typeof events == 'string') {
			if (!this.events[event]) {
				this.events[event] = true;
				this.handlers[event] = [];
			}
			var handler = {
				fn : fn,
				scope : scope,
				once : once
			};
			this.handlers[event].push(handler);
			// 绑定数组
		} else if (Object.prototype.toString.call(events) == '[object Array]') {
			for (var i = 0; i < events.length; i++) {
				event = events[i];
				if (!this.events[event]) {
					this.events[event] = true;
					this.handlers[event] = [];
				}
				this.handlers[event].push({
							fn : handler,
							scope : scope,
							once : once
						});
			}
		}
	}
	app.prototype.trigger = function(event) {
		var handlers = this.handlers[event];
		var args = Array.prototype.slice.call(arguments, 1);
		if (!handlers || handlers.length <= 0) {
			return;
		}
		for (var i = 0; i < handlers.length; i++) {
			handler = handlers[i];
			var result = handler.fn.apply(handler.scope || null, args);
			if (handler.once === true) {
				handlers.splice(handlers.indexOf(handler), 1);
			}
			if (result === false) {
				break;
			}
		}
	}
	app.prototype.once = function(event, fn, scope) {
		this.on(event, fn, scope, true);
	}

	$(function() {
				$("[hte-app]").each(function(index, el) {
							new Hte.App($(el));
						});
			});
})(jQuery);
